package com.yalemang.photovalut.ui.folder.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.blankj.utilcode.util.StringUtils.getString
import com.yalemang.framework.base.BaseVBAdapter
import com.yalemang.photovalut.base.BaseChickAdapter
import com.yalemang.photovalut.base.dialog.CommonBottomDialog
import com.yalemang.photovalut.base.encryption.EncryptionManager
import com.yalemang.photovalut.base.encryption.Result
import com.yalemang.photovalut.base.ext.load
import com.yalemang.photovalut.base.helper.CommonDrawableHelper
import com.yalemang.photovalut.base.sql.SQLManager
import com.yalemang.photovalut.base.sql.entity.ResFile
import com.yalemang.photovalut.databinding.ItemAllFolderBinding
import com.yalemang.photovalut.res.R.*
import com.yalemang.photovalut.ui.folder.PhotoFolderActivity

class ResFileAdapter(
    private var fragmentActivity: FragmentActivity,
    private var resFiles: ArrayList<ResFile>,
    private var color: Int
) : BaseVBAdapter<ResFile, ItemAllFolderBinding>(resFiles) {

    private val sqlManager = SQLManager.getInstance()

    override fun onBindViewHolder(
        holder: CommonViewHolder<ItemAllFolderBinding>,
        data: ResFile,
        position: Int
    ) {
        holder.binding.tvPhotoFolderName.text = resFiles[position].encryptionTime.toString()
        holder.binding.tvPhotoFolderNumber.text = "" + resFiles[position].length
        holder.binding.ivPhotoMenu.tag = position

        EncryptionManager.getInstance()
            .decryptView(resFiles[position].path)
            .subscribe(object: Result<ByteArray>(){
                override fun result(t: ByteArray) {
                    holder.binding.ivPhotoFolder.load(t)
                }
            })

        holder.binding.rlPhoto.background = CommonDrawableHelper.Builder().setBottom(8f).setColor(color).builder()
        holder.itemView.setOnClickListener{
            if (itemClickListener != null) {
                itemClickListener.invoke(data, position)
            }
        }
        holder.binding.ivPhotoMenu.setOnClickListener{

            CommonBottomDialog.Builder()
                .attachContext(fragmentActivity)
                .setTitle(getString(string.main_bottom_dialog_title))
                .addTab(getString(string.main_bottom_dialog_delete))
                .addTab(getString(string.main_bottom_dialog_unHide_and_restore))
                .addTab(getString(string.main_bottom_dialog_move))
                .addTab(getString(string.main_bottom_dialog_cancel))
                .setColor(UserManager.instance.user.skin.mainColor)
                .setTabClickListener {
                    bottomDialogClick(it, position)
                }
                .builder()
                .show(fragmentActivity)
        }
    }

    private fun bottomDialogClick(tab: CommonBottomDialog.Tab, position: Int){
        when(tab.content){
            getString(string.main_bottom_dialog_delete) ->{
                sqlManager.removePhotoFile(PhotoFolderActivity.folderName, resFiles[position].fileFolderName)
                resFiles.removeAt(position)
                notifyItemRemoved(position)
            }
            getString(string.main_bottom_dialog_unHide_and_restore) -> {

            }
            getString(string.main_bottom_dialog_move) ->{

            }
            getString(string.main_bottom_dialog_cancel) ->{

            }
        }
    }
}